package com.begye.smartloan.apptest.splash

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.databinding.AtSplashBinding
import com.begye.smartloan.apptest.disclosure.DisclosureActivity
import com.begye.smartloan.apptest.ext.checkNotAgreePermissions
import com.begye.smartloan.apptest.fangkuanzhong.FangKuanZhongActivity
import com.begye.smartloan.apptest.gongong.SysConfigViewModel
import com.begye.smartloan.apptest.gongong.YongHuLiuChengViewModel
import com.begye.smartloan.apptest.homeadsource.HomeAdSourceActivity
import com.begye.smartloan.apptest.homebefore.HomeBeforeActivity
import com.begye.smartloan.apptest.huankuan.HuanKuanActivity
import com.begye.smartloan.apptest.jujue.JuJueActivity
import com.begye.smartloan.apptest.shenhezhong.ShenHeZhongActivity
import com.begye.smartloan.apptest.signinup.ui.SignInUpActivity
import com.begye.smartloan.apptest.splash.dialog.ErrorDialogFragment
import com.begye.smartloan.apptest.tool.LoginTool
import com.begye.smartloan.apptest.tool.PageTagTool
import com.begye.smartloan.apptest.tool.SysConfigTool
import com.begye.smartloan.apptest.tool.addTouch
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity


@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {

    private lateinit var _binding: AtSplashBinding

    private val _sysConfigViewModel by lazy {
        ViewModelProvider(this)[SysConfigViewModel::class.java]
    }

    private val _yongHuLiuChengViewModel by lazy {
        ViewModelProvider(this)[YongHuLiuChengViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtSplashBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIData()
        configUIEvent()
    }

    /**
     * server-->config-->permission-->login
     */
    private fun configUIState() {

        _sysConfigViewModel.onServerSuccessfulLiveData.observe(this) {
            it?.let { result ->
                if (result.isNotBlank()) {
                    _sysConfigViewModel.requestSystemConfiguration()
                } else {
                    val dialog = ErrorDialogFragment {
                        configUIData()
                    }
                    dialog.showNow(supportFragmentManager, System.currentTimeMillis().toString())
                }
            }
        }

        _sysConfigViewModel.onSysConfigErrorLiveData.observe(this) {
            if (it == true) {
                val dialog = ErrorDialogFragment {
                    configUIData()
                }
                dialog.showNow(supportFragmentManager, System.currentTimeMillis().toString())
            }
        }
        _sysConfigViewModel.onSysConfigSuccessfulLiveData.observe(this) {
            it?.let { _ ->
                val notAgreeList = checkNotAgreePermissions(SysConfigTool.filterQiDongYeQuanXiang())
                if (notAgreeList.isEmpty()) {
                    if (LoginTool.filterSession().isNotBlank()) {
                        _yongHuLiuChengViewModel.requestYongHuLiuCheng()

                    } else {
                        toNextActivity(
                            activity = SignInUpActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                } else {
                    // show permission dialog
                    toNextActivity(
                        activity = DisclosureActivity::class.java,
                        mode = IntentFlagMode.NEW_TASK
                    )
                }
            }
        }
    }

    private fun configYongHuLiuChengState() {
        _yongHuLiuChengViewModel.onHasOrderStatusLiveData.observe(this) {
            it?.let { result ->
                when (result) {
                    PageTagTool.dai_huan_kuan -> {
                        toNextActivity(
                            activity = HuanKuanActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.fang_kuan_zhong -> {
                        toNextActivity(
                            activity = FangKuanZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.shen_he_zhone -> {
                        toNextActivity(
                            activity = ShenHeZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.ju_jue -> {
                        toNextActivity(
                            activity = JuJueActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                }
            }
        }

        _yongHuLiuChengViewModel.onAllDoneLiveData.observe(this) {
            it?.let { _ ->
                if (LoginTool.filterAdType()) {
                    toNextActivity(
                        activity = HomeAdSourceActivity::class.java,
                        mode = IntentFlagMode.SINGLE_TOP
                    )
                } else {
                    toNextActivity(
                        activity = HomeBeforeActivity::class.java,
                        mode = IntentFlagMode.SINGLE_TOP
                    )
                }
            }
        }
    }

    private fun configUIEvent() {
        _binding.atSplashNext.setOnClickListener {
            configUIData()
        }
    }

    private fun configUIData() {
        _sysConfigViewModel.requestServer()
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

